package com.kaifamiao.service;

import com.kaifamiao.dao.AccountDao;
import com.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lucien-ma
 * @date 2021/8/31
 * @apiNote
 */
@Service("accountService")
public class AccountService {

    @Autowired
    private AccountDao accountDao;

    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void transfer(Integer source, Integer target, Double money) {

        // 从第一个人的账户中减去 money
        accountDao.updateMoney(source, -money);

        // 模拟出现异常
        int i = 2 / 0;

        // 给二个人的账户加上 money
        accountDao.updateMoney(target, money);
    }

}
